#
# photo.py - general routes for handling photos for profiles etc
#
# Copyright 2008-2009 Autelic Association (http://www.autelic.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi, sys, os, logging
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import images
from google.appengine.ext import db
from google.appengine.ext import webapp
import wsgiref.handlers

   
class photo(webapp.RequestHandler):
  def post(self):
    error_message=''

    # TODO, generalize and break out most of this into a function other handlers can use
    # Handle the photo_add post method for the control panel page
    if self.request.get("action") == "photo_add":
       # get the photo object from form and resize
       file_name = self.request.get("photo")
       if file_name:
          try:
             photo = images.resize(filename, 70, 70)
          except:
             error_message = "Error processing photo. Please check size and format"
             self.redirect('/in/cpanel?error_message=%s' % error_message)

          try: # get correct user profile from the Datastore
             q = db.GqlQuery("SELECT * FROM Profile WHERE user = :1", users.get_current_user())
          except:
             error_message = "Problem with Gql query for Profile to datastore"
             self.redirect('/in/cpanel?error_message=%s' % error_message)
   
          try:
             profile = q.get()
          except:
             error_message = "Problem getting profile from datastore"
             self.redirect('/in/cpanel?error_message=%s' % error_message)

          try:
             profile.photo = db.Blob(photo)   # convert photo to a blob type 
          except:
             error_message = "Error converting photo to blob type object"
             self.redirect('/in/cpanel?error_message=%s' % error_message)

          try:
             db.put(profile)		   # store photo in profile in datastore
          except:
             error_message = "Error storing profile with photo in datastore"
             self.redirect('/in/cpanel?error_message=%s' % error_message)
       else:
          error_message = "No file."
       
       # now the photo is added, continue to the control panel page
       # TODO get thsi url from the header itself and make this function more general purpose
       self.redirect('/in/cpanel?error_message=%s' % error_message)

  def get(self):
     # profile ID in DB is passed as parameter called "imd_id"
     profile = db.get(self.request.get("photo_id"))      
     if profile.photo:
        self.response.headers['Content-Type'] = "image/png"
        self.response.out.write(profile.photo)
     else:
        self.response.out.write("Photo missing")

####### Main
def main(argv):
  application = webapp.WSGIApplication(
    [('/in/photo', photo)],
    debug=True)
    
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main(sys.argv)
